﻿using app.Api;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using app.Models;
using AmuTools;
using System.Data;

namespace app.Controllers.branch
{
    public class ApiListedCompany: ApiBase
    {
        public ApiListedCompany(Controller ctx) : base(ctx) { }

        internal void listedCompany(string action2)
        {
            switch (action2)
            {
                case "add":
                    {
                        ListedCompany com = GetObjectParam < ListedCompany > (new ReqObjectParamOption<ListedCompany> { Name = "listed_company" } );
                        if (UserCode != null)
                        {
                            com.company_id = Int32.Parse(UserCode);
                        }
                        else
                        {
                            com.company_id = 0;
                        }
                        com.status = "0";
                        com.number = "lc" + DateTimeEx.GetNowTimeStamp()+com.company_id.ToString().PadLeft(6,'0');
                        int value = G.DB.Insert<ListedCompany>(com);
                        if (value != 0)
                        {
                            ResponseJson(new BooleanResult { value = true, message = "信息新建成功！" });
                        }
                        else
                        {
                            ResponseJson(new BooleanResult { value = false,message = "信息新建失败！"});
                        }
                        break;
                    }
                case "getListInfo":
                    {
                        Cd cd = new Cd();
                        cd.And("company_id='{0}'", UserCode);
                        ResponseJson(GetPageResult<ListedCompany>(cd.Render(), "id desc"));
                        break;
                    }
                case "change":
                    {
                        ListedCompany c = GetObjectParam<ListedCompany>(new ReqObjectParamOption<ListedCompany> { Name = "listed_company" });
                        int value = G.DB.Update<ListedCompany>(c);
                        if (value != 0)
                        {
                            ResponseJson(new BooleanResult { value = true, message = "修改信息成功！" });
                        }
                        else
                        {
                            ResponseJson(new BooleanResult { value = false, message = "修改信息失败！" });
                        }
                        break;
                    }
                case "delete":
                    {
                        int value = G.DB.Delete<ListedCompany>(req_id);
                        if (value != 0)
                        {
                            ResponseJson(new BooleanResult { value = true, message = "信息删除成功！" });
                        }
                        else
                        {
                            ResponseJson(new BooleanResult { value = true, message = "信息删除失败！" });
                        }
                        break;
                    }
            }
        }

        internal void AdminlistedCompany(string action2)
        {
            switch (action2)
            {
                case "getlist":
                    {
                        string number = GetStringParam(new ReqStringParamOption { Name = "number" });
                        string title = GetStringParam(new ReqStringParamOption { Name = "title" });
                        string listed_sector = GetStringParam(new ReqStringParamOption { Name = "listed_sector" });
                        string list_status = GetStringParam(new ReqStringParamOption { Name = "list_status" });
                        Cd cd = new Cd();
                        if (number != "") cd.And("number like '%{0}%'", number);
                        if (req_begin_time != "") cd.And("schedule>='{0}'", req_begin_time);
                        if (req_end_time != "") cd.And("schedule<='{0}'", req_end_time);
                        if (title != "") cd.And("company_name like '%{0}%'", title);
                        if (listed_sector != "全部" && listed_sector != "境外板块")
                        {
                            cd.And("listed_sector='{0}'", listed_sector);
                        }
                        else if (listed_sector == "境外板块")
                        {
                            cd.And("board='{0}'", "abroad");
                        }
                        if (list_status != "全部") cd.And("list_status='{0}'",list_status);
                        if (req_excel)
                        {
                            DataTable dt = GetSqlResult<ListedCompany>(cd.Render(), "id desc").FirstTable;
                            ResponseExcel(dt, ListedCompany.ExcelColumns, ListedCompany.ExcelColumnsHandle);
                        }
                        else
                        {
                            ResponseJson(GetPageResult<ListedCompany>(cd.Render(), "id desc"));
                        }
                        break;
                    }
                case "add":
                    {
                        ListedCompany com = GetObjectParam<ListedCompany>(new ReqObjectParamOption<ListedCompany> { Name = "listed_company" });
                        com.company_id = 0;
                        com.status = "0";
                        com.number = "lc" + DateTimeEx.GetNowTimeStamp() + com.company_id.ToString().PadLeft(6, '0');
                        int value = G.DB.Insert<ListedCompany>(com);
                        if (value != 0)
                        {
                            ResponseJson(new BooleanResult { value = true, message = "信息新建成功！" });
                        }
                        else
                        {
                            ResponseJson(new BooleanResult { value = false, message = "信息新建失败！" });
                        }
                        break;
                    }
                case "delete":
                    {
                        int value = G.DB.Delete<ListedCompany>(req_id);
                        if (value != 0)
                        {
                            ResponseJson(new BooleanResult { value = true, message = "信息删除成功！" });
                        }
                        else
                        {
                            ResponseJson(new BooleanResult { value = true, message = "信息删除失败！" });
                        }
                        break;
                    }
                case "change":
                    {
                        ListedCompany c = GetObjectParam<ListedCompany>(new ReqObjectParamOption<ListedCompany> { Name = "listed_company" });
                        int value = G.DB.Update<ListedCompany>(c);
                        if (value != 0)
                        {
                            ResponseJson(new BooleanResult { value = true, message = "修改信息成功！" });
                        }
                        else
                        {
                            ResponseJson(new BooleanResult { value = false, message = "修改信息失败！" });
                        }
                        break;
                    }
                case "un_passed":
                    {
                        ListedCompany c = G.DB.GetById<ListedCompany>(req_id);
                        c.status = "2";
                        int value = G.DB.Update<ListedCompany>(c);
                        if (value != 0)
                        {
                            ResponseJson(new BooleanResult { value = true, message = "审核完毕！" });
                        }
                        else
                        {
                            ResponseJson(new BooleanResult { value = false,message = "审核失败"});
                        }
                        break;
                    }
                case "passed":
                    {
                        ListedCompany c = G.DB.GetById<ListedCompany>(req_id);
                        c.status = "1";
                        int value = G.DB.Update<ListedCompany>(c);
                        if (value != 0)
                        {
                            ResponseJson(new BooleanResult { value = true, message = "审核完毕！" });
                        }
                        else
                        {
                            ResponseJson(new BooleanResult { value = false, message = "审核失败" });
                        }
                        break;
                    }
                case "cancel_check":
                    {
                        ListedCompany c = G.DB.GetById<ListedCompany>(req_id);
                        c.status = "0";
                        int value = G.DB.Update<ListedCompany>(c);
                        if (value != 0)
                        {
                            ResponseJson(new BooleanResult { value = true, message = "取消审核成功！" });
                        }
                        else
                        {
                            ResponseJson(new BooleanResult { value = false, message = "取消审核失败！" });
                        }
                        break;
                    }
                default:break;
            }
        }

    }
}